package com.yykj.order.center.server.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yykj.order.center.client.dto.ConsultationDoctorListDto;
import com.yykj.order.center.client.dto.ConsultationListDto;
import com.yykj.order.center.client.dto.base.PaginationListDto;
import com.yykj.order.center.client.form.consultation.ConsultationAdminDoctorListForm;
import com.yykj.order.center.client.form.consultation.ConsultationAdminDoctorReviewForm;
import com.yykj.order.center.client.form.consultation.ConsultationAdminListForm;
import com.yykj.order.center.client.form.consultation.ConsultationAdminReviewForm;
import com.yykj.order.center.server.entity.Consultation;
import com.yykj.order.center.server.entity.ConsultationDoctor;
import com.yykj.order.center.server.enums.ConsultationDoctorStatus;
import com.yykj.order.center.server.enums.ConsultationStatus;
import com.yykj.order.center.server.exception.ErrorCode;
import com.yykj.order.center.server.exception.ErrorCodeException;
import com.yykj.order.center.server.repository.ConsultationDoctorMapper;
import com.yykj.order.center.server.repository.ConsultationMapper;
import com.yykj.order.center.server.service.ConsultationAdminService;
import com.yykj.order.center.server.service.ConsultationService;
import com.yykj.order.center.server.service.MessageService;
import com.yykj.order.center.server.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

@Component
@Transactional
public class ConsultationAdminServiceImpl implements ConsultationAdminService {

    @Autowired
    ConsultationMapper consultationMapper;

    @Autowired
    ConsultationDoctorMapper consultationDoctorMapper;

    @Autowired
    MessageService messageService;

    @Autowired
    ConsultationService consultationService;

    @Override
    public PaginationListDto<ConsultationListDto> list(ConsultationAdminListForm consultationAdminListForm) {
        PageHelper.startPage(consultationAdminListForm.getPageNum(), consultationAdminListForm.getPageSize());
        Page<Consultation> consultationList = this.consultationMapper.findList(consultationAdminListForm);
        ArrayList<ConsultationListDto> consultationListDtos = new ArrayList<>();
        for (Consultation consultation : consultationList) {
            ConsultationListDto consultationListDto = new ConsultationListDto();
            BeanUtils.copyProperties(consultation, consultationListDto);
            consultationListDto.setConsultationId(consultation.getId());
            consultationListDtos.add(consultationListDto);
        }
        PaginationListDto<ConsultationListDto> consultationListDtoPaginationListDto = new PaginationListDto<>();
        consultationListDtoPaginationListDto.setList(consultationListDtos);
        consultationListDtoPaginationListDto.setPageNum(consultationAdminListForm.getPageNum());
        consultationListDtoPaginationListDto.setPageSize(consultationAdminListForm.getPageSize());
        consultationListDtoPaginationListDto.setTotal(consultationList.getTotal());
        return consultationListDtoPaginationListDto;
    }

    @Override
    public PaginationListDto<ConsultationDoctorListDto> doctorList(ConsultationAdminDoctorListForm consultationAdminDoctorListForm) {
        PageHelper.startPage(consultationAdminDoctorListForm.getPageNum(), consultationAdminDoctorListForm.getPageSize());
        Page<ConsultationDoctorListDto> consultationDoctorList = this.consultationDoctorMapper.findList(consultationAdminDoctorListForm.getDoctorId(), consultationAdminDoctorListForm.getHospitalId(), consultationAdminDoctorListForm.getStatus());
        PaginationListDto<ConsultationDoctorListDto> consultationListDtoPaginationListDto = new PaginationListDto<>();
        consultationListDtoPaginationListDto.setList(consultationDoctorList);
        consultationListDtoPaginationListDto.setPageNum(consultationAdminDoctorListForm.getPageNum());
        consultationListDtoPaginationListDto.setPageSize(consultationAdminDoctorListForm.getPageSize());
        consultationListDtoPaginationListDto.setTotal(consultationDoctorList.getTotal());
        return consultationListDtoPaginationListDto;
    }

    @Override
    public void doctorReview(ConsultationAdminDoctorReviewForm consultationAdminDoctorReviewForm) {
        Consultation consultation = this.consultationMapper.findById(consultationAdminDoctorReviewForm.getConsultationId());
        ConsultationDoctor consultationDoctor = this.consultationDoctorMapper.findByConsultationIdAndDoctorId(consultationAdminDoctorReviewForm.getConsultationId(), consultationAdminDoctorReviewForm.getDoctorId());
        if (consultationDoctor == null) {
            throw new ErrorCodeException(ErrorCode._1002);
        }
        String result = "通过";
        if (1 == consultationAdminDoctorReviewForm.getResult()) {
            consultationDoctor.setStatus(ConsultationDoctorStatus.reviewSuccess);

        } else {
            result = "不通过";
            consultationDoctor.setStatus(ConsultationDoctorStatus.reviewFail);
        }
        consultationDoctor.setReviewRemark(consultationAdminDoctorReviewForm.getRemark());
        consultationDoctor.setReviewTime(StringUtil.getCurrentTime());
        this.consultationDoctorMapper.updateById(consultationDoctor);
        this.messageService.sendSysMeg(consultationDoctor.getDoctorImId(),
                consultation.getApplyDoctorName() + "邀请参加" + consultation.getPatientName() + " 的会诊已审核" + result);
        if (1 == consultationAdminDoctorReviewForm.getResult()) {
            ArrayList<String> memberList = new ArrayList<>();
            memberList.add(consultationDoctor.getDoctorImId());
            this.messageService.memberAdd(consultation.getGroupImId(), memberList);//添加成员
            this.messageService.sendMeg(consultation.getApplyDoctorImId(), consultationDoctor.getDoctorImId(),
                    ConsultationServiceImpl.getAddConsultationDoctorContent(consultation.getApplyDoctorName(), consultationDoctor.getDoctorName()));
        }
    }

    @Override
    public void review(ConsultationAdminReviewForm consultationAdminReviewForm) {
        Consultation consultation = this.consultationMapper.findById(consultationAdminReviewForm.getConsultationId());
        String result = "通过";
        if (1 == consultationAdminReviewForm.getResult()) {//通过
            consultation.setStatus(ConsultationStatus.reviewSuccess);
        } else {//失败
            result = "不通过";
            consultation.setStatus(ConsultationStatus.reviewFail);
        }
        consultation.setRemark(consultationAdminReviewForm.getRemark());
        consultation.setReviewTime(StringUtil.getCurrentTime());

        //建群聊天室
        consultation.setGroupImId(this.consultationService.createGroup(consultation));
        this.consultationMapper.updateById(consultation);
        ArrayList<String> memberList = new ArrayList<>();
        //发送邀请医生信息

        List<ConsultationDoctor> consultationDoctors = this.consultationDoctorMapper.findByConsultationId(consultation.getId());
        for (ConsultationDoctor consultationDoctor : consultationDoctors) {
            memberList.add(consultationDoctor.getDoctorImId());
            this.messageService.sendMeg(consultation.getApplyDoctorImId(), consultationDoctor.getDoctorImId(),
                    ConsultationServiceImpl.getAddConsultationDoctorContent(consultation.getApplyDoctorName(), consultationDoctor.getDoctorName()));
        }

        this.messageService.memberAdd(consultation.getGroupImId(), memberList);//添加成员
        this.messageService.sendSysMeg(consultation.getApplyDoctorImId(),
                consultation.getPatientName() + " 的会诊已审核" + result);
    }

    @Override
    public void doctorChargebackReview(ConsultationAdminDoctorReviewForm consultationAdminDoctorReviewForm) {
        Consultation consultation = this.consultationMapper.findById(consultationAdminDoctorReviewForm.getConsultationId());
        String result = "通过";
        if (1 == consultationAdminDoctorReviewForm.getResult()) {//通过
            consultation.setStatus(ConsultationStatus.reviewChargebackSuccess);
        } else {//失败
            result = "不通过";
            consultation.setStatus(ConsultationStatus.reviewChargebackFail);
        }
        consultation.setChargebackReviewRemark(consultationAdminDoctorReviewForm.getRemark());
        consultation.setChargebackReviewTime(StringUtil.getCurrentTime());
        this.consultationMapper.updateById(consultation);
        this.messageService.sendSysMeg(consultation.getApplyDoctorImId(),
                consultation.getPatientName() + " 的会诊退单已审核" + result);
    }
}
